-- INCLUDES
require "luasql.odbc";

-- TODO: figure out how to get the local path
dofile("/usr/local/freeswitch/scripts/SEPROJECT/paths.lua");
dofile("/usr/local/freeswitch/scripts/SEPROJECT/function_in_se.lua");
--dofile("/usr/local/freeswitch/scripts/IIITD/function.lua");

script_name = "project37.lua";
digits = "";
arg = {};
GlobalCnt = 0;
start="N";
Ending = "N"
sessid = os.time();
userid = 'nil';
adminforums = {};
line_num = session:getVariable("destination_number");
aosd = basedir .. "/scripts/IIITD/sounds/niharika";		
phonenum = session:getVariable("caller_id_number");
--phonenum = phonenum:sub(-10);
input="";
rep = "N";		
logfile:write(sessid, "\t",
		    session:getVariable("caller_id_number"), "\t", session:getVariable("destination_number"), "\t", 
		    os.time(), "\t", "Call Started", "\t", "\n");




------------------------------------------------------------------------------------------------------------------------
-------validate caller--------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

function authorize_caller()
 session:execute("set_audio_level", "write 1");
	while (session:ready() == true) do

		
   		query = "select * from table_studentdata where Col_mobileNo = " .. phonenum ;
   		freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   		cur = assert (con:execute(query));
   		row = {};result = cur:fetch(row);
   		cur:close();

                
		if (tostring(row[3]) == 'nil') then
      			role = "unregistered_number";
                        freeswitch.consoleLog("info", "unregistered");
		end
		if (tostring(row[3]) == phonenum) then
      			role="registered_number";
		end			
		break;
	end
end

------------------------------------------------------------------------------------------------------------------------
-------Registered User Menu---------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------


function registered_number()

        session:answer();
        session:setAutoHangup(false);
        --freeswitch.consolelog("registered number");
        freeswitch.consoleLog("info", "registered" .. " : actual caller_id " .. phonenum .. "\n");
        --While loop will come
        while(session:ready()==true) do

              digits=session:read(4,4,aosd .. "/prompts/disclaimer.wav",3000,"#");
              freeswitch.consoleLog("info",digits);

                query = "select * from table_studentdata where Col_mobileNo = " .. phonenum ;
   	        freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   		cur = assert (con:execute(query));
   		row = {};result = cur:fetch(row);
   		cur:close();
                local counter=0;
                local flag=0
                while(true) do
              
              
                      if(counter==2)then
                         flag=1;
                         break;
                      end
    
                      if(digits==tostring(row[4]))then
                           freeswitch.consoleLog("info", "you are have succesfully authenticated yourself".. "\n");
                           --call the marks function
                           break;
                      else
                      digits="";
                      digits=session:read(4,4,aosd .. "/prompts/disclaimer.wav",3000,"#"); --tone for wrong pin enter again
                      freeswitch.consoleLog("info",digits);
                      counter=counter+1;
                      end
              
              
               end
       
            if(flag==1) then
                 --play error message limits exceeded
                 session:execute("set_audio_level", "write 4");
                 session:say("56","en","number","pronounced");
                 session:execute("set_audio_level", "write 0");
            end
        end

end



------------------------------------------------------------------------------------------------------------------------
-------UnRegistered User Menu---------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

function unregistered_number()

        session:answer();
        session:setAutoHangup(false);
        freeswitch.consoleLog("info", "unregistered2");
        --file for unregistered number
        
       digits=session:read(4,4,aosd .. "/prompts/details.wav",3000,"#");
       freeswitch.consoleLog("info",digits);
       local num=digits
       
       query = "select * from table_studentdata where Col_mobileNo = " .. num ;
   		freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   		cur = assert (con:execute(query));
   		row = {};result = cur:fetch(row);
   		cur:close();
        if (tostring(row[3]) == num) then
      			--session:read(1,1,aosd .. "/prompts/nextA.wav",500,"#");
             digits="";
             digits=session:read(4,4,aosd .. "/prompts/disclaimer.wav",3000,"#");
             freeswitch.consoleLog("info",digits);

        query = "select * from table_studentdata where Col_mobileNo = " .. num ;
   		freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   		cur = assert (con:execute(query));
   		row = {};result = cur:fetch(row);
   		cur:close();
       local counter=0;
       local flag=0
       while(true) do
              
              
              if(counter==2)then
                 flag=1;
                 break;
              end
    
              if(digits==tostring(row[4]))then
                  freeswitch.consoleLog("info", "you are have succesfully authenticated yourself".. "\n");
                  --call the marks function
                  get_marks(tostring(row[5]))
                  break;
              else
                  digits="";
                  digits=session:read(4,4,aosd .. "/prompts/disclaimer.wav",3000,"#"); --tone for wrong pin enter again
                  freeswitch.consoleLog("info",digits);
                  counter=counter+1;
              end
              
              
       end
       
       if(flag==1) then
             --play error message limits exceeded
             session:execute("set_audio_level", "write 4");
             session:say("56","en","number","pronounced");
             session:execute("set_audio_level", "write 0");
       end
                            
        end
end




function get_marks(rollno)

      query = "select * from table_studentmarks where Col_rollnumber = " .. rollno ;
   		freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   		cur = assert (con:execute(query));
   		row = {};result = cur:fetch(row);
   		cur:close();
      if(tostring(row[1])==rollno) then
          freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
          session:execute("set_audio_level", "write 4");
             session:say("12","en","number","pronounced");
             session:execute("set_audio_level", "write 0");
      end
      session:execute("set_audio_level", "write 4");
             session:say(tostring(row[2]),"en","number","iterated");
             session:say(tostring(row[2]),"en","number","pronounced");
             session:execute("set_audio_level", "write 0");
end
----------------------------------------------------------------------------------------------------
------------START---START----START----START----START----START---------------------------------------
----------------------------------------------------------------------------------------------------

session:answer();
session:setVariable("playback_terminators", "#");
session:setHangupHook("hangup");
session:setInputCallback("my_cb", "arg");
--freeswitch.consoleLog("info", script_name .. " : debug " .. "\n");
authorize_caller();
freeswitch.consoleLog("info", script_name .. " : actual caller_id " .. phonenum .. "\n");
session:setVariable("phonenum", phonenum);
--generate a menu
session:set_tts_parms("flite", "rms");
while (session:ready() == true) do
	
	read(basedir .. "/scripts/IIITD/sounds/tune.wav",100);	
	
	if (role == "registered_number") then
		registered_number();
	end
	if (role == "unregistered_number") then
		
		unregistered_number();
--			
	end
freeswitch.consoleLog("info", "did you reach here\n");
		session:read(1,1,aosd .."/prompts/thanx.wav",200,"#");	
	break;
	
end
session:hangup();
